package com.jsj.service.impl;

import com.jsj.mapper.DeviceMapper;
import com.jsj.mapper.RepairMapper;
import com.jsj.pojo.Repair;
import com.jsj.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RepairServiceImpl implements RepairService {

    @Autowired
    RepairMapper repairMapper;
    @Autowired
    DeviceMapper deviceMapper;

    @Override
    public Boolean addRepair(Repair repair) {
        //将device_status更改为维修0
        String deviceStatus="维修中";
        Boolean aBoolean = deviceMapper.updateDeviceStatus(repair.getDeviceId(), deviceStatus);
        //将审核状态更改未审核
        repair.setCheckStatus("未审核");
        //将维修状态改为未维修
        repair.setRepairStatus("未审核");
        //添加维修
        Boolean flag = repairMapper.addRepair(repair);
        if (aBoolean){
            flag=true;
            return true;
        }
        return false;
    }
//查询全部维修信息
    @Override
    public List<Repair> queryAllRepair() {
        List<Repair> repairs = repairMapper.queryAllRepair();
        return repairs;
    }
//关键字搜索
    @Override
    public List<Repair> queryRepairByName(String deviceName) {
        List<Repair> repairs = repairMapper.queryRepairByName(deviceName);
        return repairs;
    }
//修改维修信息
    @Override
    public Boolean updateRepairById(Repair repair) {
        Boolean flag = repairMapper.updateRepairById(repair);
        return flag;
    }

    @Override
    public Boolean repairedAndReturn(int repairId, int deviceId, Date returnTime) {
        //将设备状态改为正常
        deviceMapper.updateDeviceStatus(deviceId,"正常");
        //将维修状态repairStatus改为维修完毕,
        Boolean flag = repairMapper.repairedAndReturn(repairId,returnTime,"已维修");
        return flag;
    }

    @Override
    public List<Repair> queryRepairByStatus(String status) {
        if (status.equals("未审核")||status.equals("已维修")) {
            System.out.println("-----status--未审核/已维修执行了--");
            List<Repair> repairs = repairMapper.queryRepairByStatus(status);
            return repairs;
        }
        if (status.equals("已审核")){
            System.out.println("========status==已审核====");
            List<Repair> repairs = repairMapper.queryRepairByStatus2("维修中","拒绝");
            return repairs;
        }
        return null;


    }

    @Override
    public Boolean delRepairById(int repairId) {
        Boolean flag = repairMapper.delRepairById(repairId);
        return flag;
    }
//根据已审核，未审核，搜索
    @Override
    public List<Repair> queryCheckStatus(String status) {
        if (status.equals("未审核")) {
            System.out.println("-----status--未审核执行了--");
            List<Repair> repairs = repairMapper.queryCheckStatus(status);
            return repairs;
        }
        if (status.equals("已审核")){
            System.out.println("========status==已审核====");
            List<Repair> repairs = repairMapper.queryChecked("同意","拒绝");
            return repairs;
        }
        return null;
    }
//审核请求，同意或拒绝
    @Override
    public Boolean checkRepair(int repairId,String check) {
        //将审核状态check_Status更改为“同意”或“拒绝”
        //check:同意或拒绝
        Boolean flag = repairMapper.checkRepair(repairId,check);
        //同意或拒绝后，同时更改repair_satus状态
        if (check.equals("同意")){
            Boolean aBoolean = repairMapper.updateRepairStatus(repairId,"维修中");
        }
        if (check.equals("拒绝")){
            Boolean aBoolean = repairMapper.updateRepairStatus(repairId,check);
        }

        return flag;
    }

    @Override
    public Boolean updateRepairDeviceStatus(int repairId, int deviceId) {

        //将设备状态改为正常
        Boolean aBoolean = deviceMapper.updateDeviceStatus(deviceId, "正常");
        //删除该条被拒绝的记录
        Boolean flag = repairMapper.delRepairById(repairId);
        if (aBoolean){
            flag=true;
            return true;
        }
        return false;

    }


}
